//package clientServer;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.IOException;

import javax.swing.JOptionPane;

import common.ChatIF;

/**
 * 
 * inherited class
 *
 */
public class ClientQuery implements ChatIF 
{
  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
  
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
  public ChatClient client;

  
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientQuery(String host, int port) 
  {
    try 
    {
      client= new ChatClient(host, port, this);
          
    }//try 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!"
                + " Terminating client.");
      JOptionPane.showMessageDialog(null,"Error: Can't setup connection to server! Terminating client.\n" +
      		"please check the server IP and try again.\n" +
      		"for any further help, please contact your administrator.","CCRM Error",JOptionPane.ERROR_MESSAGE);
    }//catch
  }//ClientQuery

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input .Once it is 
   * received, it sends it to the client's message handler.
   */
  public synchronized void accept(String message) 
  {
      client.handleMessageFromClientUI(message);
  }//accept

  /**
   * This method overrides the method in the ChatIF interface.  It
   * displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(String message) 
  {
    System.out.print(message+" ");
  }//display

  public String getRs()
  {
	  String temp="";
	  while(ChatClient.result=="");
	      temp=(String)ChatClient.result;
	      ChatClient.result="";
	  return temp;
  }//getRs   
}//ClientQuery
//End of ClientQuery class
